package ui;

import java.util.Date;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.DateField;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;

import manager.AgentManagerMIDlet;


import java.util.Vector;

public class MeetingAgentEntryScreen extends Form implements CommandListener{
    private final AgentManagerMIDlet midlet;
    //private final Form fmMain;
    //private final TextBox tbAction;
    private final Command exitCommand;
    private final Command myCommand1;
    private final Command myCommand2;
    private final Command confirmCommand;
    private final DateField timeEntry;
    private final Display display;
    private final Date currentTime;
    private int timeIndex;
    private int totalTimes; 
    private Vector times;

    MeetingAgentEntryScreen(AgentManagerMIDlet midlet, Display display, String string, 
    		int timeIndex, int totalTimes, Vector times)
    {
        super("Enter the  time number " + timeIndex );
        
        if(times!=null){
        	this.times=times;
        }else{
        	this.times = new Vector(totalTimes);
        }
        this.display = display;
        this.timeIndex = timeIndex;
        this.totalTimes = totalTimes;
        this.midlet = midlet;
        exitCommand = new Command("Exit", Command.EXIT, 1);
        //Save today's date
        currentTime = new Date();
        
        timeEntry = new DateField("",DateField.DATE_TIME);
        timeEntry.setDate(currentTime);
        
        //fmMain = new Form("Core J2ME");
        this.append(timeEntry);
        myCommand1 = new Command("myCommand1", Command.SCREEN, 2);
        myCommand2 = new Command("myCommand2", Command.SCREEN, 3);
        confirmCommand = new Command("Confirm", Command.SCREEN,1);
        
        this.addCommand(confirmCommand);
        this.addCommand(exitCommand);
        this.addCommand(myCommand1);
        this.addCommand(myCommand2);
        this.setCommandListener(this);
    }


    public void commandAction(Command c, Displayable d)
    {
        if (c == exitCommand)
        {
            midlet.exitRequested();
        }else if(c == myCommand1){
        //	midlet.displayNext();
        }else if(c ==confirmCommand){
            this.times.addElement(timeEntry);
        	//go through the meeting agent screens recursively
        	if((++timeIndex)<=totalTimes){
	        	MeetingAgentEntryScreen meetingAgentEntryScreen = 
	    			new MeetingAgentEntryScreen(midlet,
	    					display,
	    					"MeetingAgentEntry",
	    					timeIndex,
	    					totalTimes,
	    					this.times);
	        	display.setCurrent(meetingAgentEntryScreen);
        	}else{
        		//gotten all times
        		FinalMeetingAgentEntry finalMeetingAgentEntry = 
        			new FinalMeetingAgentEntry(midlet,display,times);
        		display.setCurrent(finalMeetingAgentEntry);
        	}
        }
    }
}
